package net.huashitong.giszd.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.giszd.dao.EarlyWarningNodeDao;
import net.huashitong.giszd.entity.EarlyWarningNode;
import net.huashitong.giszd.entity.NodeNewtemplet;
import net.huashitong.giszd.filter.EarlyWarningNodeFilter;
import net.huashitong.giszd.filter.NodeNewtempletFilter;
import net.huashitong.giszd.responseModel.EarlyWarningCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.*;

@Service
public class EarlyWarningNodeService extends StringPKService<EarlyWarningNode> {
    @Override
    protected BaseDao<EarlyWarningNode, String> getDao() {
        return null;
    }
    @Autowired
    private EarlyWarningNodeDao dao;
    @Autowired
    private NodeNewtempletService nodeNewtempletService;


    /**
     * 根据项目id获取各个预警等级的数量
     * @param projectId
     * @return
     */
    public Map<String, Integer> queryByWarningNumber(String projectId) {
        EarlyWarningNodeFilter filter = new EarlyWarningNodeFilter();
        if (StringUtils.isNotBlank(projectId)) {
            int num = 0;
            int total;
            int low = 0;
            int mid = 0;
            int high = 0;
            filter.setProjectId(projectId);
            List<EarlyWarningNode> earlyWarningNodes = this.queryEarlyWarning(filter);
            total = earlyWarningNodes.size();
            for (EarlyWarningNode ewn : earlyWarningNodes) {
                num = Integer.parseInt(ewn.getNumber());
                if (num > 0 && num <= 5) {
                    low += 1;
                }
                if (num > 5 && num <= 10) {
                    mid += 1;
                }
                if (num > 10) {
                    high += 1;
                }
            }
            Map<String, Integer> map = new HashMap<>();
            map.put("low", low);
            map.put("mid", mid);
            map.put("high", high);
            map.put("total", total);
            return map;
        } else {
            return null;
        }
    }


    public List<EarlyWarningNode> queryEarlyWarning(EarlyWarningNodeFilter filter) {
        if (filter.getPage() == null) {
            filter.setPage(1);
        }
        Paging p = new Paging();
        p.setCurrentPage(filter.getPage());
        filter.setPaging(p);
        return dao.queryEarlyWarning(filter);
    }

    /**
     * 根据项目Id跟等级获取预警详情
     * @param projectId
     * @param level
     * @return
     */
    public List<EarlyWarningNode> queryWarningDetail(String projectId, String level) {
        EarlyWarningNodeFilter filter = new EarlyWarningNodeFilter();
        if (StringUtils.isNotBlank(projectId) && StringUtils.isNotBlank(level)) {
            filter.setProjectId(projectId);
            List<EarlyWarningNode> earlyWarningNodes = this.queryEarlyWarning(filter);
            int num = 0;
            List<EarlyWarningNode> low = new ArrayList<>();
            List<EarlyWarningNode> mid = new ArrayList<>();
            List<EarlyWarningNode> high = new ArrayList<>();
            List<EarlyWarningNode> resList = new ArrayList<>();

            for (EarlyWarningNode ewn : earlyWarningNodes) {
                num = Integer.parseInt(ewn.getNumber());
                if (num > 0 && num <= 5) {
                    low.add(ewn);
                }
                if (num > 5 && num <= 10) {
                    mid.add(ewn);
                }
                if (num > 10) {
                    high.add(ewn);
                }
            }
            if (level.equals("1")) {
                for (EarlyWarningNode ewn : low) {
                    String nodeId = ewn.getNodeId();
                    for(EarlyWarningNode e : earlyWarningNodes){
                        if(nodeId.equals(e.getNodeId())){
                            resList.add(e);
                        }
                    }
                }
            }
            if (level.equals("2")) {
                for (EarlyWarningNode m : mid) {
                    String nodeId = m.getNodeId();
                    for(EarlyWarningNode e : earlyWarningNodes){
                        if(nodeId.equals(e.getNodeId())){
                            resList.add(e);
                        }
                    }
                }
            }
            if (level.equals("3")) {
                for (EarlyWarningNode h : high) {
                    String nodeId = h.getNodeId();
                    for(EarlyWarningNode e : earlyWarningNodes){
                        if(nodeId.equals(e.getNodeId())){
                            resList.add(e);
                        }
                    }
                }
            }
            return resList;
        } else {
            return null;
        }
    }

    /**
     * 根据项目Id获取所有预警列表
     * @param projectId
     * @return
     */
    public List<EarlyWarningNode> queryByWarningNumberShort(String projectId) {
        EarlyWarningNodeFilter filter = new EarlyWarningNodeFilter();
        if (StringUtils.isNotBlank(projectId)) {
            filter.setProjectId(projectId);
            List<EarlyWarningNode> earlyWarningNodes = this.queryEarlyWarning(filter);
            return earlyWarningNodes;
        } else {
            return null;
        }
    }

    /**
     * 统计各部门预警次数
     * @return
     */
    public List<Map<String,Object>> queryCalculationDept(){
        List<EarlyWarningCount> resList = dao.queryCalculationDept();
        BigInteger eachCount = null;
        for(EarlyWarningCount earlyWarningCount : resList){
            if("业主、中介机构".equals(earlyWarningCount.getResDept())){
                eachCount = earlyWarningCount.getCount();
                break;
            }
        }

        List<Map<String,Object>> list = new ArrayList<>();
        for(EarlyWarningCount earlyWarningCount : resList){
            if ("业主、中介机构".equals(earlyWarningCount.getResDept())) {
                continue;
            }
            if ("业主单位".equals(earlyWarningCount.getResDept())) {
                Map<String,Object> map1 = new HashMap<>();
                BigInteger b1 = earlyWarningCount.getCount();
                map1.put("resDept",earlyWarningCount.getResDept());
                map1.put("count",b1.add(eachCount));
                list.add(map1);
                continue;
            }
            if ("中介机构".equals(earlyWarningCount.getResDept())) {
                BigInteger b2 = earlyWarningCount.getCount();
                Map<String,Object> map = new HashMap<>();
                map.put("resDept",earlyWarningCount.getResDept());
                map.put("count",b2.add(eachCount));
                list.add(map);
                continue;
            }
            Map<String,Object> map = new HashMap<>();
            map.put("resDept",earlyWarningCount.getResDept());
            map.put("count",earlyWarningCount.getCount());
            list.add(map);
        }
        return list;
    }



    /**
     * 统计各部门预警次数
     * @return
     */
    public Map<String,Object> queryCalculationNodeId(){
        List<EarlyWarningCount> resList = dao.queryCalculationNodeId();
        Map<String,Object> resultMap = new HashMap<>();
        Map<String,BigInteger> map = new HashMap<>();
        for(EarlyWarningCount earlyWarningCount : resList){
            String nodeId = earlyWarningCount.getNodeId().substring(0,earlyWarningCount.getNodeId().lastIndexOf("."));
            if(ObjectUtils.isBlank(map.get(nodeId))){
                map.put(nodeId,earlyWarningCount.getCount());
            }else{
                map.put(nodeId,earlyWarningCount.getCount().add(map.get(nodeId)));
            }
        }
        //设置获取大节点  type等于1
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        filter.setType("1");
        List<NodeNewtemplet> nodeNewtemplets = nodeNewtempletService.getNodeTempletInfo(filter);
        Map<String,String> templetsMap = new TreeMap<>();
        for(NodeNewtemplet t : nodeNewtemplets){
            if(!templetsMap.containsKey(t.getNodeId())){
                templetsMap.put(t.getNodeId(),t.getNodeName());
            }
        }
        List<String> nodeNameList = new ArrayList<>();
        List<BigInteger> countList = new ArrayList<>();
        for(String strKey : templetsMap.keySet()){
            String nodeName = templetsMap.get(strKey);
            BigInteger count = map.get(strKey);
            if(ObjectUtils.isBlank(count)){
                continue;
            }
            nodeNameList.add(nodeName);
            countList.add(count);
        }
        resultMap.put("nodeNameList",nodeNameList);
        resultMap.put("countList",countList);
        return resultMap;
    }
}
